package com.camel.network.controller;

import cn.hutool.json.JSONObject;
import com.camel.core.config.CtiConfig;
import com.camel.core.entity.Result;
import com.camel.core.utils.HttpUtils;
import com.camel.core.utils.ResultUtil;
import com.camel.network.config.CtiConstants;
import com.camel.network.config.UrlConstants;
import com.camel.network.model.MpccListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 86183
 */
@RequestMapping("/tasks")
@RestController
public class AutoCallController extends NetBaseController{

    @Autowired
    private CtiConfig ctiConfig;

    /**
     * 查询外呼计划
     *
     * @param params
     * @return
     */
    @PostMapping
    public Result index(@RequestBody JSONObject params) {
        JSONObject json = (JSONObject) HttpUtils.post(params, ctiConfig.getBaseUrl(), UrlConstants.CTI_GET_TASKS_URL);
        return ResultUtil.success(getList(json));
    }

    /**
     * 创建外呼计划
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody JSONObject params) {
        JSONObject post = (JSONObject) HttpUtils.post(params, ctiConfig.getBaseUrl(), UrlConstants.CTI_CREATE_TASK_URL);
        if (post.get(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, "保存任务失败");
        }
    }

    @PostMapping("/del")
    public Result del(@RequestBody JSONObject params) {
        JSONObject post = (JSONObject) HttpUtils.post(params, "tj.svdata.cn", UrlConstants.CTI_DELETE_TASK_URL);
        if (post.get(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, "删除任务失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody JSONObject params) {
        JSONObject post = (JSONObject) HttpUtils.post(params, ctiConfig.getBaseUrl(), UrlConstants.CTI_TASK_CONFIG_URL);
        if (post.get(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, "修改任务参数失败");
        }
    }
}
